package gui

import scala.swing.ScrollPane
import scala.swing.event._
import java.io.File
import javax.swing.tree.DefaultMutableTreeNode

/**
 * Diese Klasse erweitert ein Scrollpane und
 * fuegt diesem einen Baum hinzu.
 * 
 * Die Klasse wird verwendet um den Verzeichnisbaum
 * im linken Bereich des Programms zur Verfuegung zu
 * stellen. Entsprechend sind Funktionen zum Laden
 * und Uebertragen der Verzeichnisinhalte in den Baum
 * vorhanden.
 */

class ScrollTreePane (tt: Layout) extends ScrollPane {
  
  var tree = new Tree(tt)

  val dir = System.getProperty("user.dir")
  var root = new File(dir)
  var head = tree.addNode(root, tree.getHead())
  fillTree(root, head)

  tree.expandAll(true)

  contents = tree
  peer.getVerticalScrollBar().setUnitIncrement(16)

  def refillTree(): Unit = {
    var tmp = new Thread(new Runnable(){
        override def run() = {
          tree = new Tree(tt)
          root = new File(dir)
          head = tree.addNode(root, tree.getHead())
          head.removeAllChildren
          fillTree(root, head) 
          tree.revalidate
        }
      })
    tmp.start()
    tree.repaint
    this.repaint
    tt.repaint
  }
  
  def fillTree(file: File, head: DefaultMutableTreeNode): Unit = {
      var liste = file.list
      for(i <- 0 to liste.size-1) {
        var tmp = new File(file + "/" + liste(i).toString)
        if(tmp.isDirectory && containsImportantFiles(tmp)) {
          var unter = tree.addNode(liste(i), head)
          fillTree(tmp, unter)
        }
        else if(!tmp.isDirectory) {
          var addd = false
          for(j <- 0 to CONST.FILE_TYPES(Layout.prolamode).size-1) {
            if(liste(i).endsWith(CONST.FILE_TYPES(Layout.prolamode).get(j)))
              addd = true
          }
          if(addd == true) {
            var node = tree.addNode(liste(i), head)
          }
        }
      }
  }
  
  def containsImportantFiles(file: File): Boolean = {
    var ret = false
    var liste = file.list
    for(i <- 0 to liste.size-1) {
      var tmp = new File(file + "/" + liste(i).toString)
      if(tmp.isDirectory) {
        ret = ret || containsImportantFiles(tmp)
      }
      else {
        for(j <- 0 to CONST.FILE_TYPES_DIR(Layout.prolamode).size-1) {
          if(liste(i).endsWith(CONST.FILE_TYPES_DIR(Layout.prolamode).get(j)))
            ret = true
        }
      }
    }
    return ret
  }
  
  def getTree() : Tree = tree
  
  def setTree(t:Tree) = {
    tree = t
  }  
  
}


 